/**
 * Smart City use case October 2012 
 */
package com.orange.smartcity.android.activity;

import static com.orange.smartcity.android.SmartCityCommon.*;

import com.orange.smartcity.android.R;
import com.orange.smartcity.android.SmartCityApplication;
import com.orange.smartcity.android.SmartCityCommon.KINDS;
import com.orange.smartcity.android.tools.ApplicationMenuUtils;
import com.orange.smartcity.android.tools.Flip3dAnimationUtils;
import com.orange.smartcity.android.tools.SwapViews;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.CompoundButton.OnCheckedChangeListener;

/**
 * @author FAIROUZ OUBENALI
 * 
 */
public class EventList extends Activity {
	

	private boolean isFirstImage = true;

	/**
	 * Called when the activity is first created. Responsible for initializing
	 * the UI.
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.i(EVENT_LIST_ACTIVITY_TAG, "**** Smart City Main UI lunched ****");
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.event_list);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);
		currentActivity = EVENT_LIST_ACTIVITY_TAG;
		SmartCityApplication.setAppContext(this);
		// button bar
		mFooterSetting = (ImageView) findViewById(R.id.imageSetting);
		mFooterActions = (ImageView) findViewById(R.id.imageAction);
		mFooterEventList = (ImageView) findViewById(R.id.ImageEvents);
		mLayoutFooterSettings = (LinearLayout) findViewById(R.id.layout_setting);
		mLayoutFooterEvents = (LinearLayout) findViewById(R.id.layout_eventList);
		mLayoutFooterActions = (LinearLayout) findViewById(R.id.layout_action);
		// footer background color
		setFooterEventBackground();
		// Animation hyperspaceJump = AnimationUtils.loadAnimation(this,
		// R.anim.hyperspace_jump);
		// image.startAnimation(hyperspaceJump);
		mFooterSetting
				.setOnClickListener(new ItemClickListener(KINDS.SETTINGS));

		mFooterActions.setOnClickListener(new ItemClickListener(KINDS.ACTIONS));
		mFooterEventList.setOnClickListener(new ItemClickListener(
				KINDS.EVENT_LIST));
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {

		if (keyCode == KeyEvent.KEYCODE_BACK) {
			getHome(SmartCityApplication.getAppContext());
			finish();
			return true;
		}
		return false;
	}

	/*
	 * method used by clicking on the list of item
	 */
	// ===========================================================================================//
	class ItemClickListener implements OnClickListener, OnCheckedChangeListener {

		private Long id = null;
		private int type = 0;
		private KINDS kind;

		public ItemClickListener(int type) {
			super();
			this.type = type;
		}

		public ItemClickListener(KINDS kind) {
			super();
			this.kind = kind;
		}

		public void onClick(View mView) {
			switch (kind) {
			case SETTINGS:
				if (!currentActivity.equals(HOME_ACTIVITY_TAG)) {
					getHome(SmartCityApplication.getAppContext());
					setFooterHomeBackground();
				}
				animation(mFooterSetting, isFirstImage);
				break;

			case ACTIONS:
				if (!currentActivity.equals(ACTION_ACTIVITY_TAG)) {
					getActions(SmartCityApplication.getAppContext());
					setFooterActionBackground();
				}
				animation(mFooterActions, isFirstImage);

				break;

			case EVENT_LIST:
				if (!currentActivity.equals(EVENT_LIST_ACTIVITY_TAG)) {
					getEventList(SmartCityApplication.getAppContext());
					setFooterEventBackground();
				}
				animation(mFooterEventList, isFirstImage);
				break;
			}

		}

		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {
			// TODO Auto-generated method stub

		}

	}

	/*
	 * ==========================================================================
	 * = * footer bar * *
	 * ======================================================================
	 */

	public void setFooterHomeBackground() {
		mLayoutFooterSettings.setBackgroundResource(R.color.transparent);
		mLayoutFooterEvents.setBackgroundResource(R.color.holo_orange_light);
		mLayoutFooterActions.setBackgroundResource(R.color.holo_orange_light);

	}

	public void setFooterActionBackground() {
		// background color
		mLayoutFooterActions.setBackgroundResource(R.color.transparent);
		mLayoutFooterSettings.setBackgroundResource(R.color.holo_orange_light);
		mLayoutFooterEvents.setBackgroundResource(R.color.holo_orange_light);
	}

	public void setFooterEventBackground() {
		// background color
		mLayoutFooterEvents.setBackgroundResource(R.color.transparent);
		mLayoutFooterSettings.setBackgroundResource(R.color.holo_orange_light);
		mLayoutFooterActions.setBackgroundResource(R.color.holo_orange_light);
	}

	/*
	 * ==========================================================================
	 * = * menu * *
	 * ======================================================================
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.menu, menu);
		return true;
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		ApplicationMenuUtils.onPrepareOptionsMenu(this, menu);
		return super.onPrepareOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		ApplicationMenuUtils.onOptionsItemSelected(this, item);
		return true;
	}

	/*
	 * ==========================================================================
	 * = * animation * *
	 * ======================================================================
	 */

	private void animation(ImageView image1, boolean isFirstImage) {
		if (isFirstImage) {
			applyRotation(0, 90, image1, image1);
			isFirstImage = !isFirstImage;

		} else {
			applyRotation(0, -90, image1, image1);
			isFirstImage = !isFirstImage;
		}

	}

	private void applyRotation(float start, float end, ImageView image1,
			ImageView image2) {
		// Find the center of image
		final float centerX = image1.getWidth() / 2.0f;
		final float centerY = image1.getHeight() / 2.0f;

		// Create a new 3D rotation with the supplied parameter
		// The animation listener is used to trigger the next animation
		final Flip3dAnimationUtils rotation = new Flip3dAnimationUtils(start,
				end, centerX, centerY);
		rotation.setDuration(500);
		rotation.setFillAfter(true);
		rotation.setInterpolator(new AccelerateInterpolator());
		rotation.setAnimationListener(new DisplayNextView(isFirstImage, image1,
				image2));

		if (isFirstImage) {
			image1.startAnimation(rotation);
		} else {
			image2.startAnimation(rotation);
		}

	}

	final class DisplayNextView implements Animation.AnimationListener {
		private boolean mCurrentView;
		ImageView image1;
		ImageView image2;

		public DisplayNextView(boolean currentView, ImageView image1,
				ImageView image2) {
			mCurrentView = currentView;
			this.image1 = image1;
			this.image2 = image2;
		}

		public void onAnimationStart(Animation animation) {
		}

		public void onAnimationEnd(Animation animation) {
			image1.post(new SwapViews(mCurrentView, image1, image2));
		}

		public void onAnimationRepeat(Animation animation) {
		}
	}

	/*
	 * ==========================================================================
	 * = * methods * *
	 * ======================================================================
	 */

	/*
	 * activity setting
	 */
	public void getSettings() {
		Intent preferences = new Intent(this, Settings.class);
		preferences.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
		startActivity(preferences);
	}
}
